<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="关键字" prop="docKeyWord">
        <el-input
          v-model="queryParams.docKeyWord"
          placeholder="文档名称、简介、上传人、备注中的相关关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文档类型" prop="docuTypeId">
        <el-select
          v-model="queryParams.docuTypeId"
        >
          <el-option
            v-for="{ label, value } in docTypeList"
            :label="label"
            :value="value"
            :key="value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="文档简介" prop="docuDesc">
        <el-input
          v-model="queryParams.docuDesc"
          placeholder="请输入文档简介"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="文档版本" prop="docuVersion">
        <el-input
          v-model="queryParams.docuVersion"
          placeholder="请输入文档版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="文档minio地址" prop="docuAddr">
        <el-input
          v-model="queryParams.docuAddr"
          placeholder="请输入文档minio地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="添加人" prop="docuCreater">
        <el-input
          v-model="queryParams.docuCreater"
          placeholder="请输入添加人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="添加时间" prop="docuCreateTime">
        <el-date-picker
          clearable
          v-model="queryParams.docuCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择添加时间"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="指定文档角色组(与其他权限校验字段是或的关系)" prop="docuRole">
        <el-input
          v-model="queryParams.docuRole"
          placeholder="请输入指定文档角色组(与其他权限校验字段是或的关系)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="指定文档部门组(与其他权限校验字段是或的关系)" prop="docuDept">
        <el-input
          v-model="queryParams.docuDept"
          placeholder="请输入指定文档部门组(与其他权限校验字段是或的关系)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="指定人员查看(与其他权限校验字段是或的关系)" prop="docuUser">
        <el-input
          v-model="queryParams.docuUser"
          placeholder="请输入指定人员查看(与其他权限校验字段是或的关系)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否删除(1:存在;0:删除)" prop="docuDel">
        <el-input
          v-model="queryParams.docuDel"
          placeholder="请输入是否删除(1:存在;0:删除)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="备注" prop="docuTemp">
        <el-input
          v-model="queryParams.docuTemp"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:document:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:document:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:document:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:document:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="documentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文档类型ID" align="center" prop="docuTypeId" />
      <el-table-column label="文档名称" align="center" prop="docuName" />
      <el-table-column label="文档简介" align="center" prop="docuDesc" />
      <el-table-column label="文档版本" align="center" prop="docuVersion" />
      <el-table-column label="文档minio地址" align="center" prop="docuAddr" />
      <el-table-column label="添加人" align="center" prop="docuCreater" />
      <el-table-column
        label="添加时间"
        align="center"
        prop="docuCreateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.docuCreateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="指定文档角色组(与其他权限校验字段是或的关系)"
        align="center"
        prop="docuRole"
      /> -->
      <!-- <el-table-column
        label="指定文档部门组(与其他权限校验字段是或的关系)"
        align="center"
        prop="docuDept"
      /> -->
      <!-- <el-table-column
        label="指定人员查看(与其他权限校验字段是或的关系)"
        align="center"
        prop="docuUser"
      /> -->
      <!-- <el-table-column
        label="是否删除(1:存在;0:删除)"
        align="center"
        prop="docuDel"
      /> -->
      <el-table-column label="备注" align="center" prop="docuTemp" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:document:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:document:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文档详情对话框 -->
    <edit-dialog
      :is-show.sync="dialogOpt.isShow"
      :dialog-type="dialogOpt.type"
      :docu-id="dialogOpt.docuId"
      @close="changeDialogShow(false)"
      @updateList="getList"
    ></edit-dialog>
  </div>
</template>

<script>
import { listDocument, delDocument } from "@/api/doc/document";
import EditDialog from "./EditDialog.vue";
import DIALOG_TYPE from "@/utils/const.js"

export default {
  name: "Document",
  components: {
    EditDialog,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文档详情表格数据
      documentList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        docuTypeId: null,
        docuName: null,
        docuDesc: null,
        docuVersion: null,
        docuAddr: null,
        docuCreater: null,
        docuCreateTime: null,
        docuRole: null,
        docuDept: null,
        docuUser: null,
        docuDel: null,
        docuTemp: null,
        docKeyWord: null,
      },
      // 表单参数
      form: {},
      // 弹窗配置
      dialogOpt: {
        isShow: false,
        type: "add",
      },
      // 文档类型列表
      docTypeList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文档详情列表 */
    getList() {
      this.loading = true;
      listDocument(this.queryParams).then((response) => {
        this.documentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 更改弹窗状态
    changeDialogShow(show) {
      this.dialogOpt.show = show;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.docuId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.dialogOpt.isShow = true;
      this.dialogOpt.type = DIALOG_TYPE.ADD;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // getDocument(docuId).then((response) => {
      //   this.form = response.data;
      //   this.title = "修改文档详情";
      // });
      this.dialogOpt.docuId = row.docuId || this.ids;
      this.dialogOpt.isShow = true;
      this.dialogOpt.type = DIALOG_TYPE.UPDATE;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const docuIds = row.docuId || this.ids;
      this.$modal
        .confirm('是否确认删除文档详情编号为"' + docuIds + '"的数据项？')
        .then(function () {
          return delDocument(docuIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/document/export",
        {
          ...this.queryParams,
        },
        `document_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
